package czk.concrent.enjoyModelWithNotLock.unsafe.assignToArray;

import czk.concrent.enjoyModelWithNotLock.unsafe.UnsafeAccessor;
import sun.misc.Unsafe;

/**
 * 给数组赋值
 * unsafe底层操作数组
 *
 * @Author:ChenZhangKun
 * @Date: 2021/5/28 9:52
 */
public class AssignToArray {
    public static void main(String[] args) {
        // 拿到Unsafe底层操作类
        Unsafe unsafe = UnsafeAccessor.getUnsafe();
        // 创建数组
        long[] array = new long[4];
        // 拿到数组第一个元素的偏移量
        int baseOffset = unsafe.arrayBaseOffset(long[].class);
        // 拿到数组的每个元素的大小
        int scale = unsafe.arrayIndexScale(long[].class);
        // 拿到第三个个元素的偏移量
        long offset = baseOffset + scale * 2;
        // 赋值
        unsafe.putOrderedLong(array, offset, 50);
        // 输出值
        System.out.println(array[2]);
    }
}
